package ex.recruit.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyBase;
import modules.recruit.entity.ApplyBg;
import modules.recruit.entity.ApplyConfirm;
import modules.recruit.entity.ApplyDivide;
import modules.recruit.entity.ApplyEquity;
import modules.recruit.entity.ApplyFinance;
import modules.recruit.entity.ApplyManager;
import modules.recruit.entity.ApplyPlan;
import modules.recruit.entity.Region;
import modules.recruit.entity.RmProvince;
import modules.recruit.entity.ShareholderA;
import modules.recruit.entity.ShareholderB;
import modules.recruit.entity.ShareholderC;
import modules.recruit.form.ApplyBaseForm;
import modules.recruit.form.ApplyConfirmForm;
import modules.recruit.form.ApplyEquityForm;
import modules.recruit.form.ApplyFinanceForm;
import modules.recruit.form.ApplyManagerForm;
import modules.recruit.form.ApplyPlanForm;
import modules.recruit.form.ShareholderAForm;
import modules.recruit.form.ShareholderBForm;
import modules.recruit.form.ShareholderCForm;
import modules.recruit.service.ApplyConfirmService;
import modules.recruit.service.ApplyDivideService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import site.user.form.ApplyBaseFormSite;

import com.guinea.core.Constants;
import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.orm.Page;
import com.guinea.core.orm.PropertyFilter;
import com.guinea.core.service.EntityService;
import com.guinea.core.utils.ServletUtils;

import ex.recruit.service.RmProvinceServiceEx;

public class ApplyConfirmActionEx extends CommonApplyJudgment {
	
	
	ApplyConfirmService applyConfirmService;
//	ApplyDivideService applyDivideService;
//	RmProvinceServiceEx rmProvinceServiceEx;

	public EntityService getEntityService() {
		return applyConfirmService;
	}

	public ApplyConfirmService getApplyConfirmService() {
		return applyConfirmService;
	}

	public void setApplyConfirmService(ApplyConfirmService applyConfirmService) {
		this.applyConfirmService = applyConfirmService;
	}

	

//	public ApplyDivideService getApplyDivideService() {
//		return applyDivideService;
//	}
//
//	public void setApplyDivideService(ApplyDivideService applyDivideService) {
//		this.applyDivideService = applyDivideService;
//	}
//
//
//
//	public RmProvinceServiceEx getRmProvinceServiceEx() {
//		return rmProvinceServiceEx;
//	}
//
//	public void setRmProvinceServiceEx(RmProvinceServiceEx rmProvinceServiceEx) {
//		this.rmProvinceServiceEx = rmProvinceServiceEx;
//	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		if (entity == null && applyBase != null) {
			entity = applyBase.getConfirm();
		}
	}
	
	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				FILTER_PREFIX + "_");
		List filters = PropertyFilterForSql.buildFromParaMap(filterParamMap);
		
		filters.add(new PropertyFilterForSql("step", "1"));
		Page page = PropertyFilter.buildPageFromHttpRequest(request,
				REQUEST_PAGE);
		
		page = getApplyBaseServiceEx().getApplyProcess(page, filters, true);

		request.setAttribute(REQUEST_PAGE, page);
		request.setAttribute(FILTER_MAP, filterParamMap);
		return mapping.findForward(LIST);
	}
	
	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		bindForm(form, entity);
		ApplyConfirmForm applyConfirmForm = (ApplyConfirmForm)form;
		applyConfirmForm.setApplyBaseId(applyBaseId);
		
		request.setAttribute("apply",applyBase);
		return handleEdit(mapping,form,request,response);
	}
	
	protected void doInitEntity(HttpServletRequest request,ActionForm form) {
		super.doInitEntity(request, form);
		if (isCreateOperation) {
			ApplyConfirm applyConfirm = (ApplyConfirm) entity;
			applyConfirm.setBase(applyBase);
			entity = applyConfirm;
		}
	}
	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		doInitEntity(request, form);

		doSaveEntity(request, form);
//		//自动分配
//		ApplyDivide applyDivide = applyBase.getDivide();
//		if(applyDivide == null){
//			applyDivide = new ApplyDivide();
//			applyDivide.setBase(applyBase);
//		}
//		Set regions = applyBase.getRegion();
//		String provinceId = null;
//		for (Iterator iter = regions.iterator(); iter.hasNext();) {
//			Region region = (Region) iter.next();
//			if("1".equals(region.getSerial())){
//				provinceId = region.getProvinceId();
//				break;
//			}
//		}
//		Long managerId = null;
//		if(provinceId != null){
//			List rmProvinces = getRmProvinceServiceEx().get();
//			for (Iterator iter = rmProvinces.iterator(); iter.hasNext();) {
//				RmProvince rmProvince = (RmProvince) iter.next();
//				if(provinceId.equals(rmProvince.getProvinceId())){
//					managerId = rmProvince.getOwner();
//				}
//			}
//		}
//		applyDivide.setOwner(managerId);
//		getApplyDivideService().saveOrUpdate(applyDivide);

		return mapping.findForward(LIST);
	}

	public ActionForward splitValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (isCancelled(request)){
			valid = false;
			return mapping.findForward(LIST);
		}

		initFirst(form, request);
		if (!validateApplyBase())
			return handleEntityMissing(mapping, form, request, response);
		Set regions = applyBase.getRegion();
		int count = 0;
		for (Iterator iter = regions.iterator(); iter.hasNext();) {
			Region r = (Region) iter.next();
			if("1".equals(r.getStatus()))
				count++ ;
		}
		if(count < 2){
			valid = false;
			saveDirectlyError(request,"计划场地少于2个，不能拆分。");
			return handleEdit(mapping, form, request, response);
		}

		return null;
	}

	public ActionForward split(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ApplyBase applyBaseDes = new ApplyBase();
		Region secondRegion = null;
		Set regions = applyBase.getRegion();
		for (Iterator iter = regions.iterator(); iter.hasNext();) {
			Region region = (Region) iter.next();
			if("1".equals(region.getStatus()) && "2".equals(region.getSerial())){
				secondRegion = region;
				break;
			}
		}
		if(secondRegion == null){
			saveDirectlyError(request,"未找到第 2 个计划场地，不能拆分。");
			return handleEdit(mapping, form, request, response);
		}
		ApplyBaseForm applyBaseFormTemp = new ApplyBaseForm();
		bindForm(applyBaseFormTemp,applyBase);
		bindEntity(applyBaseFormTemp,applyBaseDes);
		applyBaseDes.setId(null);
		applyBaseDes.setSubmitDate(applyBase.getSubmitDate());
		applyBaseDes.setSerial(getApplyBaseServiceEx().getSerial(secondRegion.getProvinceId()));
		getApplyBaseServiceEx().save(applyBaseDes);
		
		//保存原创建人
		applyBaseDes.setCreatedDate(applyBase.getCreatedDate());
		applyBaseDes.setCreatedUser(applyBase.getCreatedUser());
		getApplyBaseServiceEx().update(applyBaseDes);
		
		secondRegion.setBase(applyBaseDes);
		secondRegion.setSerial("1");
		getApplyBaseServiceEx().update(secondRegion);
		
		ApplyEquity applyEquityDes = new ApplyEquity();
		applyEquityDes.setBase(applyBaseDes);
		ApplyEquity applyEquitySrc = applyBase.getEquity();
		saveSrc(applyEquitySrc,applyEquityDes,new ApplyEquityForm());
		
		Set shareholderAs = applyEquitySrc.getShareholderA();
		for (Iterator iter = shareholderAs.iterator(); iter.hasNext();) {
			ShareholderA shareholderASrc = (ShareholderA) iter.next();
			ShareholderA shareholderADes = new ShareholderA();
			shareholderADes.setEquity(applyEquityDes);
			saveSrc(shareholderASrc,shareholderADes,new ShareholderAForm());
		}
		
		Set shareholderBs = applyEquitySrc.getShareholderB();
		for (Iterator iter = shareholderBs.iterator(); iter.hasNext();) {
			ShareholderB shareholderBSrc = (ShareholderB) iter.next();
			ShareholderB shareholderBDes = new ShareholderB();
			shareholderBDes.setEquity(applyEquityDes);
			saveSrc(shareholderBSrc,shareholderBDes,new ShareholderBForm());
		}
		
		ApplyFinance applyFinanceDes = new ApplyFinance();
		applyFinanceDes.setBase(applyBaseDes);
		saveSrc(applyBase.getFinance(),applyFinanceDes,new ApplyFinanceForm());
		
		ApplyBg applyBgDes = new ApplyBg();
		applyBgDes.setBase(applyBaseDes);
		saveSrc(applyBase.getBackground(),applyBgDes,new ApplyBaseForm());
		
		ApplyPlan applyPlanDes = new ApplyPlan();
		applyPlanDes.setBase(applyBaseDes);
		ApplyPlan applyPlanSrc = applyBase.getPlan();
		saveSrc(applyPlanSrc,applyPlanDes,new ApplyPlanForm());
		
		Set shareholderCs = applyPlanSrc.getShareholderC();
		for (Iterator iter = shareholderCs.iterator(); iter.hasNext();) {
			ShareholderC shareholderCSrc = (ShareholderC) iter.next();
			ShareholderC shareholderCDes = new ShareholderC();
			shareholderCDes.setPlan(applyPlanDes);
			saveSrc(shareholderCSrc,shareholderCDes,new ShareholderCForm());
		}
		
		Set managers = applyBase.getManager();
		for (Iterator iter = managers.iterator(); iter.hasNext();) {
			ApplyManager applyManagerSrc = (ApplyManager) iter.next();
			ApplyManager applyManagerDes = new ApplyManager();
			applyManagerDes.setBase(applyBaseDes);
			saveSrc(applyManagerSrc,applyManagerDes,new ApplyManagerForm());
		}
		


		return mapping.findForward(LIST);
	}
	
	private void saveSrc(Object src,Object des, ActionForm form){
		bindForm(form,src);
		bindEntity(form,des);
		getApplyBaseServiceEx().save(des);
	}

}
